<template>
  <div>
    <el-form
      :model="form"
      ref="form"
      :rules="rules"
      label-width="80px"
      :inline="false"
      size="samll"
    >
      <el-form-item label="会员" prop="memberId">
        <el-input v-model="form.memberId"></el-input>
      </el-form-item>
      <el-form-item label="评论内容" prop="content" >
        <el-input type="textarea" v-model="form.content"></el-input>
      </el-form-item>

      <el-form-item label="课程id" prop="courseId">
       <el-input v-model="form.courseId"></el-input>
      </el-form-item>
      <el-form-item label="文章id" prop="blogId">
       <el-input v-model="form.blogId"></el-input>
      </el-form-item>
      <el-form-item style="display: flex; justify-content: flex-end">
        <el-button size="small" @click="handleClose">取消</el-button>
        <el-button size="small" type="primary" @click="onSubmit"
          >保存</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import memberCommentApi from "@/api/memberComment";
export default {
  name: "memberCommentSave",
  props: {
    memberCommentId: {
      type: String,
      default: "",
    },
    close: {
      type: Boolean,
      defalut: false,
    },
  },

  data() {
    return {
      options: [],
      CommentOptions:[],
      form: {
        id: "",
        memberId: "",
        content:"",
        courseId: "",
        blogId: "",
      },
      rules: {
        memberId: [
          { required: true, message: "会员不能为空", trigger: "blur" },
        ],
        content: [
          { required: true, message: "评论内容不能为空", trigger: "blur" },
        ],
      },
    };
  },
  watch: {
    close(newValue, oldValue) {
      this.form =  {
        id: "",
        memberId: "",
        content:"",
        courseId: "",
        blogId: "",
      };
    },
    memberCommentId(newValue, oldValue) {
      if (this.memberCommentId) this.getInfo(this.memberCommentId);
    },
  },
  created() {
    if (this.memberCommentId) this.getInfo(this.memberCommentId);

  },
  methods: {
    // 获取数据
    async getInfo(id) {
      let res = await memberCommentApi.info(id);
      if (res.success) {
        this.form = res.data.data;
      } else {
        return this.$message.error(res.msg);
      }
    },
    // 保存
    onSubmit() {
      this.$refs.form.validate((v) => {
        if (this.memberCommentId) {
          this.handleUpdate();
        } else {
          this.handleAdd();
        }
      });
    },
    // 添加
    async handleAdd() {
      let res = await memberCommentApi.add(this.form);
      if (res.success) {
        this.$message.success("添加成功");
        this.$emit("done");
      } else {
        return this.$message.error(res.msg);
      }
    },
    // 修改
    async handleUpdate() {
      let res = await memberCommentApi.update(this.form);
      if (res.success) {
        this.$message.success("修改成功");
        this.$emit("done");
      } else {
        return this.$message.error(res.msg);
      }
    },

    handleClose() {
      this.$emit("done");
    },
  },
};
</script>

<style lang="scss" scoped>
</style>